#!/sbin/openrc-run

command_user="root"

mv_db() {
   if [ -z "$(ls /var/lib/mysql)" ]; then
       echo "Moving mysql volume"
       mv /srv/http/mysql/* /var/lib/mysql/ && chown -R mysql:mysql /data/mysql &&  mv /srv/http/migrations/* /srv/http/chat/migrations/ && rm -r /srv/http/migrations && rm -r /srv/http/mysql || return 1
   fi
}

start() {
   ebegin "initing tables"
   set -x
   install -d -m 0777 -o http -g http /data/photos &&\
   install -d -m 0755 -o http -g http /data/migrations &&\
   install -d -m 0755 -o redis -g redis /data/redis &&\
   install -d -m 0755 -o mysql -g mysql /data/mysql &&\
   chmod 777 /data &&\
   chown http:http /srv/http/chat/settings.py &&\
   chown http:http /srv/http/fe/production.json &&\
   chown http:http /srv/http/photos &&\
   chown http:http /srv/http/chat/migrations &&\
   chmod 777 /dev/stdout &&\
   chmod 777 `readlink /dev/stdout` &&\
   chmod 777 /dev/stderr &&\
   chmod 777 `readlink /dev/stderr` &&\
   mv_db && \
   su - http -c '/srv/http/download_content.sh compile_js'
   res=$?
   echo "Finish status $res"
   eend $res
}
